package com.dfbz.dao;

import com.dfbz.dao.Base.BaseDao;
import com.dfbz.model.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDao extends BaseDao<User> {
    private JdbcTemplate jdbcTemplate=getJdbcTemplate();
    public UserDao() {
        super(User.class);
    }
    public List<User> findUsers(){
        return jdbcTemplate.query("select * from user",new BeanPropertyRowMapper<>(User.class));
    }
    public User findByEmail(String email){
        User user = jdbcTemplate.queryForObject("select * from user where email =?", new BeanPropertyRowMapper<>(User.class), email);
        return user;
    }
    public User findByName(String name){
        User user;
        try {
            user = jdbcTemplate.queryForObject("select * from user where username =?", new BeanPropertyRowMapper<>(User.class),name);
            return user;
        }catch (Exception e){
            return null;
        }
    }
    public void register(User user){
        jdbcTemplate.update("insert into user(id,username,password,email) value (?,?,?,?)"
                ,user.getId(),user.getUsername(),user.getPassword(),user.getEmail());
    }
    public List<User> findPage(String realName,Integer startIndex,Integer pageSize){
        List<User> list = jdbcTemplate.query("select * from user where real_name like ? limit ?,?",
                new BeanPropertyRowMapper<>(User.class),
                "%" + realName + "%", startIndex, pageSize);
        return list;
    }

    public Integer countByRealName(String realName) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from user where real_name like ?",
                    Integer.class,"%"+realName+"%");
        }catch (Exception e){
            return null;
        }
    }
    public void updateLoginTime(User user){
        jdbcTemplate.update("update user set login_time=? where id=?",user.getLoginTime(),user.getId());
    }

    public void updateLook(Integer uId) {
        jdbcTemplate.update("update user set look=look+1 where id=?",uId);
    }

    public List<User> findfocusAll(Integer id,Integer startIndex,Integer pageSize ) {
        String sql="select * " +
                "from user " +
                "where user.id in (select uf.user_focus_id as fid " +
                "from userfocus uf,user u " +
                "where uf.user_id=u.id and u.id=?) limit ?,?";
         return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),id,startIndex,pageSize);
    }
    public Integer countByfocusAll(Integer id){
        try {
            String sql="select count(1) " +
                    "from user " +
                    "where user.id in (select uf.user_focus_id as fid " +
                    "from userfocus uf,user u " +
                    "where uf.user_id=u.id and u.id=?)";
            return jdbcTemplate.queryForObject(sql,Integer.class,id);
        }catch (Exception e){
            return null;
        }
    }
}
